package com.zhao.service.serviceImpl;

import com.zhao.Others.DateAndTimeObject;
import com.zhao.dao.*;
import com.zhao.pojo.*;
import com.zhao.service.PublicizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PublicizeServiceImpl implements PublicizeService {

    @Autowired
    private PublicizeDao publicizeDao;

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private UniversityDao universityDao;

    @Autowired
    private SchoolCounsellorDao schoolCounsellorDao;

    @Autowired
    private AdminDao adminDao;

    ///admin
    @Override
    public String getOnePublicize(String publicizeID, Admin admin, HttpSession session) {

        Publicize publicize = publicizeDao.findByID(publicizeID,admin.getUniversityID());
        if(publicize == null){
            System.out.println("公告ID错误");
            return "error";
        }

        PublicizeImpl publicizeImpl = publicizeToPublicizeImpl(publicize);

        session.setAttribute("publicizeImpl",publicizeImpl);

        ///阅读数量+1
        publicize.setPageviews(publicize.getPageviews()+1);
        int res = publicizeDao.updatePageviews(publicize);

        return "success";
    }

    @Override
    public String getOnePublicize(String publicizeID, Superviser superviser, HttpSession session) {

        Publicize publicize = publicizeDao.findByID(publicizeID,superviser.getUniversityID());
        if(publicize == null){
            System.out.println("公告ID错误");
            return "error";
        }

        PublicizeImpl publicizeImpl = publicizeToPublicizeImpl(publicize);

        session.setAttribute("publicizeImpl",publicizeImpl);

        ///阅读数量+1
        publicize.setPageviews(publicize.getPageviews()+1);
        int res = publicizeDao.updatePageviews(publicize);

        return "success";
    }


    @Override
    public String adminUploadPublicize(Admin admin,Publicize publicize, HttpSession session) {

        publicize.setUniversityID(admin.getUniversityID());
        publicize.setFromID(admin.getAdminID());
        publicize.setDateandtime(DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());
        publicize.setPageviews(0);

        //生成发布单号 universityID+schoolcounsellorID+nowDate+nowTime
        publicize.setPublicizeID(admin.getUniversityID()+admin.getAdminID()
                +DateAndTimeObject.getOnlyNumberOfNowDate()+DateAndTimeObject.getOnlyNumberOfNowTime());

        int res = publicizeDao.add(publicize);
        if(res==1){
            System.out.println("success");
            return "success";
        }
        System.out.println("error");
        return "系统错误";
    }


    /////////////////////////  公共       /////////////////////////////

    private PublicizeImpl publicizeToPublicizeImpl(Publicize publicize,SchoolCounsellor schoolCounsellor){

        PublicizeImpl publicizeImpl = new PublicizeImpl();

        publicizeImpl.setPublicizeID(publicize.getPublicizeID());
        publicizeImpl.setTitle(publicize.getTitle());
        publicizeImpl.setText(publicize.getText());
        publicizeImpl.setDateandtime(publicize.getDateandtime());
        publicizeImpl.setPageviews(publicize.getPageviews());


        publicizeImpl.setFromID(publicize.getFromID());
        publicizeImpl.setFromName(schoolCounsellor.getName());

        Department department =departmentDao.findByID(schoolCounsellor.getDepartmentID(),schoolCounsellor.getUniversityID());
        publicizeImpl.setFromDepartmentName(department.getDepartmentName());

        University university = universityDao.findByID(publicize.getUniversityID());
        publicizeImpl.setUniversityID(publicize.getUniversityID());
        publicizeImpl.setUniversityName(university.getUniversityName());
        publicizeImpl.setImage(publicize.getImage());

        return publicizeImpl;
    }

    private PublicizeImpl publicizeToPublicizeImpl(Publicize publicize){

        PublicizeImpl publicizeImpl = new PublicizeImpl();

        publicizeImpl.setPublicizeID(publicize.getPublicizeID());
        publicizeImpl.setTitle(publicize.getTitle());
        publicizeImpl.setText(publicize.getText());
        publicizeImpl.setDateandtime(publicize.getDateandtime());
        publicizeImpl.setPageviews(publicize.getPageviews());
        publicizeImpl.setFromID(publicize.getFromID());
        University university = universityDao.findByID(publicize.getUniversityID());
        publicizeImpl.setUniversityID(publicize.getUniversityID());
        publicizeImpl.setUniversityName(university.getUniversityName());
        publicizeImpl.setImage(publicize.getImage());

        SchoolCounsellor schoolCounsellor = schoolCounsellorDao.findByID(publicize.getFromID(),publicize.getUniversityID());
        if(schoolCounsellor!=null){
            publicizeImpl.setFromName(schoolCounsellor.getName());
            Department department =departmentDao.findByID(schoolCounsellor.getDepartmentID(),schoolCounsellor.getUniversityID());
            publicizeImpl.setFromDepartmentName(department.getDepartmentName());

        }
        else {
            Admin admin = adminDao.findByID(publicize.getFromID(),publicize.getUniversityID());
            publicizeImpl.setFromName(admin.getName());
        }


        return publicizeImpl;
    }



    @Override
    public String getNew3Publicize(HttpSession session, String universityID) {

        List<Publicize> publicizeList = publicizeDao.getNew3Publicize(universityID);
        System.out.println(publicizeList.size()+" 条");
        //System.out.println(publicizeList);

        List<PublicizeImpl> publicizeImplList = new ArrayList<>();

        for(int i= 0 ; i < publicizeList.size() ; i++){

            PublicizeImpl publicizeImpl = publicizeToPublicizeImpl(publicizeList.get(i));
            publicizeImplList.add(publicizeImpl);
        }
        session.setAttribute("publicizeImplList3",publicizeImplList);

        return "success";
    }

    @Override
    public String getAllPublicize(String universityID,HttpSession session) {

        List<Publicize> publicizeList = publicizeDao.findAll(universityID);

        List<PublicizeImpl> allPublicizeImplList = new ArrayList<>();

        for(int i = 0 ; i < publicizeList.size() ; i++){
            PublicizeImpl publicizeImpl = publicizeToPublicizeImpl(publicizeList.get(i));
            allPublicizeImplList.add(publicizeImpl);
        }
        System.out.println(allPublicizeImplList.get(0));
        session.setAttribute("allPublicizeImplList",allPublicizeImplList);

        return "success";
    }


    @Override
    public String getOnePublicize(String publicizeID, SchoolCounsellor schoolCounsellor, HttpSession session) {

        Publicize publicize = publicizeDao.findByID(publicizeID,schoolCounsellor.getUniversityID());
        if(publicize == null){
            System.out.println("公告ID错误");
            return "error";
        }

        PublicizeImpl publicizeImpl = publicizeToPublicizeImpl(publicize,schoolCounsellor);

        session.setAttribute("publicizeImpl",publicizeImpl);

        ///阅读数量+1
        publicize.setPageviews(publicize.getPageviews()+1);
        int res = publicizeDao.updatePageviews(publicize);

        return "success";
    }

    @Override
    public String getOnePublicize(String publicizeID, Student student, HttpSession session) {

        Publicize publicize = publicizeDao.findByID(publicizeID,student.getUniversityID());
        if(publicize == null){
            System.out.println("公告ID错误");
            return "error";
        }

        PublicizeImpl publicizeImpl = publicizeToPublicizeImpl(publicize);
        System.out.println(publicizeImpl);
        session.setAttribute("publicizeImpl",publicizeImpl);

        ///阅读数量+1
        publicize.setPageviews(publicize.getPageviews()+1);
        int res = publicizeDao.updatePageviews(publicize);

        if(res!=1){
            return "error";
        }
        return "success";
    }


    /////////////////////////  辅导员       /////////////////////////////



    @Override
    public String uploadPublicize(Publicize publicize, HttpSession session) {


        SchoolCounsellor schoolCounsellor = (SchoolCounsellor) session.getAttribute("schoolCounsellor");

        if(schoolCounsellor == null){
            return "请先登录";
        }

        publicize.setUniversityID(schoolCounsellor.getUniversityID());
        publicize.setFromID(schoolCounsellor.getSchoolCounsellorID());
        publicize.setDateandtime(DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());
        publicize.setPageviews(0);

        //生成发布单号 universityID+schoolcounsellorID+nowDate+nowTime
        publicize.setPublicizeID(schoolCounsellor.getUniversityID()+schoolCounsellor.getSchoolCounsellorID()
                +DateAndTimeObject.getOnlyNumberOfNowDate()+DateAndTimeObject.getOnlyNumberOfNowTime());

        int res = publicizeDao.add(publicize);
        if(res==1){
            System.out.println("success");
            return "success";
        }
        System.out.println("error");
        return "系统错误";
    }


    @Override
    public String findAllMyPublicize(SchoolCounsellor schoolCounsellor,HttpSession session) {

        List<Publicize> publicizeList =
                publicizeDao.findAllBySchoolCounsellorID(schoolCounsellor.getSchoolCounsellorID(),schoolCounsellor.getUniversityID());

        if(publicizeList == null){
            return "success";
        }
        List<PublicizeImpl> publicizeImplList = new ArrayList<>();

        for(int i = 0 ; i < publicizeList.size() ; i++){
            PublicizeImpl publicizeImpl = publicizeToPublicizeImpl(publicizeList.get(i),schoolCounsellor);
            if(publicizeImpl!=null){
                publicizeImplList.add(publicizeImpl);
            }
        }
        System.out.println(publicizeImplList.size());
        System.out.println(publicizeImplList);
        session.setAttribute("publicizeImplList",publicizeImplList);

        return "success";
    }

    @Override
    public String deletePublicize(String publicizeID, SchoolCounsellor schoolCounsellor, HttpSession session) {

        int res = publicizeDao.deletePublicizeByID(publicizeID,schoolCounsellor.getUniversityID());

        if(res != 1){
            System.out.println("删除失败");
            return "error";
        }

        return "success";
    }

}
